/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.smarttv.ner;

import edu.yonsei.iwb.smarttv.util.SMARTTVLog;
import java.util.Vector;

/**
 *
 * @author DK
 */
public class QueryPartitionSet extends Vector<QueryPartition> {

    public QueryPartitionSet() {
    }

    public QueryPartitionSet(Vector<String> words) {
        Vector<Vector> combination = new Vector<Vector>();
        int cutpoint_size = words.size() - 1;
        for (int i = 1; i <= cutpoint_size; i++) {
            getCutPointCombination(cutpoint_size, combination, i);
        }
        QueryPartition firstqp = new QueryPartition();
        firstqp.addQueryVectorSet(words);
        this.add(firstqp);
        for (int i = 0; i < combination.size(); i++) {
            Vector<Integer> pointbyindex = combination.get(i);
            int pre_cutpoint = 0;
            QueryPartition qp = new QueryPartition();
            for (int k = 0; k < pointbyindex.size(); k++) {
                String s = new String();
                Vector<String> vector_s = new Vector<String>();
                for (int j = 0; j < words.size(); j++) {
                    if (j < pointbyindex.get(k)) {
                        if (pre_cutpoint <= j) {
                            s += "+" + words.get(j).toString() + " ";
                            vector_s.add(words.get(j).toString());
                        }
                    }
                }
                pre_cutpoint = pointbyindex.get(k);
                qp.addQueryVectorSet(vector_s);
            }
            String sr = new String();
            Vector<String> vector_sr = new Vector<String>();
            for (int k = pre_cutpoint; k < words.size(); k++) {
                sr += "+" + words.get(k).toString() + " ";
                vector_sr.add(words.get(k).toString());
            }
            qp.addQueryVectorSet(vector_sr);
            this.add(qp);
        }
        SMARTTVLog.log(SMARTTVLog.DETAIL, this.toString());
    }

    private void doExpandCombination(Vector<Integer> cutpoint, boolean[] used, int selectionNumber, int startIndex, Vector<Vector> result) {
        if (selectionNumber == 0) {
            Vector<Integer> s = new Vector<Integer>();
            for (int i = 0; i < used.length; i++) {
                if (used[i]) {
                    s.add(cutpoint.get(i));
                }
            }
            result.add(s);
            return;
        }
        for (int i = startIndex; i <= cutpoint.size() - selectionNumber; i++) {
            used[i] = true;
            doExpandCombination(cutpoint, used, selectionNumber - 1, i + 1, result);
            used[i] = false;
        }
    }

    private Vector<Vector> getCutPointCombination(int size, Vector<Vector> result, int index) {
        Vector<Integer> cutpoint = new Vector<Integer>();
        for (int c = 1; c < size + 1; c++) {
            cutpoint.add(c);
        }
        boolean[] used = new boolean[size];
        doExpandCombination(cutpoint, used, index, 0, result);
        return result;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < this.size(); i++) {
            sb.append("Partition #" + i + " of Query: " + this.get(i).toStringQueryVector() + "\n");
        }
        return sb.toString();
    }
}
